using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Text.RegularExpressions;

namespace XHTMLPoink
{
    public class XHTMLTemplate
    {

        // Variables
        Dictionary<string, string> Sections;
        Dictionary<string, string> Variables;
		public static string FilesDir;
		
        // Constructor
        public XHTMLTemplate(string File)
        {

            // Vars
            Sections = new Dictionary<string, string>();
            Variables = new Dictionary<string, string>();

            // Load File
            LoadTemplate(File);

        }

        // Load File
        public void LoadTemplate(string File)
        {

            // Clear
            if (Sections.Count > 0)
            	Sections.Clear();

            // Load File
            XmlTextReader XML = new XmlTextReader(File);

            // Go through XML File
            while (XML.Read())
                // Node?
                if (XML.NodeType == XmlNodeType.Element)
                    // Section?
                    if (XML.Name.ToLower() == "section")
                        // Store it.
                     	   Sections.Add(XML.GetAttribute("name"), XML.ReadString());
        }

        // Set Var
        public void SetVariable(string Name, string Value)
        {

			// To lower!
			Name = Name.ToLower();

            // Exist?
            if (Variables.ContainsKey(Name))
                Variables[Name] = Value;
            else
                Variables.Add(Name, Value);

        }

        // Unset var
        public void UnsetVariable(string Name)
        {

			// To lower!
			Name = Name.ToLower();

            // Exist?
            if (Variables.ContainsKey(Name))
                Variables.Remove(Name);

        }

        // Get Var
        public string GetVariable(string Name)
        {

			// To lower!
			Name = Name.ToLower();

            // Exist?
            if (Variables.ContainsKey(Name))
                return Variables[Name];
            else
                return string.Empty;

        }

        // Delete vars
        public void DeleteVariables()
        {

            // Delete all.
            Variables.Clear();

        }

        // GetSection
        public string GetSection(string Name)
        {

            // Exist?
            if (Sections.ContainsKey(Name))
            {

                // Store section.
                string str = Sections[Name];
                // Replace Vars
                str = ReplaceVariables(str); 
                
                // Return
                return str;

            }
            else
                throw new ArgumentException("This template does not contain a section '" + Name + "'.");

        }

		// Replace variables
		private string ReplaceVariables(string str)
		{
		
			// Match baby!
			MatchCollection Matches = Regex.Matches(str, @"\{(?<varname>[^\{\}]+)\}", RegexOptions.IgnoreCase);
			
			// For each match, go and replace with it's var value, if it exists,
			// else, nothingness!
			string var, val;
			foreach (Match M in Matches)
			{
				
				// Get name
				var = M.Groups["varname"].Value.ToLower();
				
				// Is there a var for it?
				if (Variables.ContainsKey(var))
					// Use it's value
					val = Variables[var];
				else
					// Empty string.
					val = string.Empty;
				
				// Replace
				str = str.Replace("{" + M.Groups["varname"].Value + "}", val);
				
			}
		
			return str;
		
		}

    }
}
